package com.zzb.projectfactory.controller.interceptor;

import com.alibaba.fastjson.JSON;
import com.zzb.projectfactory.framework.common.BaseController;
import com.zzb.projectfactory.framework.role.entity.User;
import com.zzb.projectfactory.framework.role.service.SysMenuService;
import com.zzb.projectfactory.framework.role.vo.SysMenuTreeVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户菜单拦截器
 * Created by zhangzhenbin on 17-2-28.
 */
public class UserMenuInterceptor extends BaseController implements HandlerInterceptor {
    private static final Logger logger = LoggerFactory.getLogger(UserMenuInterceptor.class);
    @Autowired
    private SysMenuService sysMenuService;
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        User currentUser = getCurrentUser(httpServletRequest);
        if(currentUser!= null && modelAndView != null){
            SysMenuTreeVO menuVO = sysMenuService.getTreeVoByUserId(currentUser.getId());
            logger.info(JSON.toJSONString(menuVO));
            modelAndView.addObject("menuVO",menuVO);
            modelAndView.addObject("currentUser",currentUser);
        }

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
